package uk.ac.cam.djjb2.mygame;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public final class Vector2i {
	public final int x;
	public final int y;
	
	public static final Vector2i ZERO = new Vector2i();
	
	public Vector2i() {
		this.x = 0;
		this.y = 0;
	}
	public Vector2i(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public Vector2i add(Vector2i other) {
		return new Vector2i(this.x + other.x, this.y + other.y);
	}
	public Vector2i add(int x, int y) {
		return new Vector2i(this.x + x, this.y + y);
	}
	public Vector2i sub(Vector2i other) {
		return new Vector2i(this.x - other.x, this.y - other.y);
	}
	public Vector2i sub(int x, int y) {
		return new Vector2i(this.x - x, this.y - y);
	}
	public Vector2i negate() {
		return new Vector2i(-this.x, -this.y);
	}
	public Vector2i mul(int other) {
		return new Vector2i(this.x * other, this.y * other);
	}
	public Vector2i mul(Vector2i other) {
		return new Vector2i(this.x * other.x, this.y * other.y);
	}
	public Vector2i div(int other) {
		return new Vector2i(this.x / other, this.y / other);
	}
	public Vector2i div(Vector2i other) {
		return new Vector2i(this.x / other.x, this.y / other.y);
	}
	public int dot(Vector2i other) {
		return this.x * other.x + this.y * other.y;
	}
	public Vector2i cross() {
		return new Vector2i(-this.y, this.x);
	}
	public int lengthSq() {
		return this.x*this.x + this.y*this.y;
	}
	public double length() {
		return Math.sqrt(this.lengthSq());
	}
	
	@Override
	public int hashCode() {
		return x + y*3571;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (obj instanceof Vector2i) {
			Vector2i other = (Vector2i) obj;
			if (x != other.x)
				return false;
			if (y != other.y)
				return false;
			return true;
		}
		return false;
	}
	public String toString() {
		return "[Vector2i ("+x+", "+y+")]";
	}
	public Vector2d toVector2d() {
		return new Vector2d(x, y);
	}
	
	public static void write(DataOutput output, Vector2i vec) throws IOException {
		output.writeInt(vec.x);
		output.writeInt(vec.y);
	}
	public static Vector2i read(DataInput input) throws IOException {
		return new Vector2i(
				input.readInt(),
				input.readInt()
				);
	}
}
